<?php
/**
 * Created by PhpStorm.
 * User: zyw
 * Date: 2018-04-08
 * Time: 11:30
 */

class Report extends MY_Controller{

    public function __construct(){
        parent::__construct();
    }

    /**
     * 列表
     */
    public function get_list(){
        $pageTitle = "举报列表";

        $status = $this->input->get_post("status") ? $this->input->get_post("status") : 1;
        $page = $this->input->get_post("page") ? $this->input->get_post("page") : 1;
        $pagesize = $this->input->get_post("pagesize") ? $this->input->get_post("pagesize") : 10;
        $where = array("status" => $status);
        $reportList = $this->ReportModel->getReportList($where, $page, $pagesize);
        $num = $this->ReportModel->getReportNum($where);
        foreach($reportList as $k=>$v){
            $list[$k]=$this->ReportModel->get_data($v['type'],$v['be_id'],$status);
        }
        $url = "index.php?c=report&m=get_list&status=".$status;
        $multipage = multipage($num, $page, $pagesize, $url);

        $this->load->view('report/get_list',array(
            "page" => $page,
            "status_s" => $status,
            "pageTitle" => $pageTitle,
            "reportList" => $list,
            "multipage" => $multipage
        ));
    }

    /**
     * 删除举报内容
     */
    public function report_del(){
        $id = $this->input->get_post("id");
        $idList = explode(",", $id);
        if(empty($idList)){
            std_out(-1, "id不能为空");
        }
        $login = getLoginUser();
        $param = array("status" => -3, "to_uid" => $login["uid"]);
        foreach($idList as $id){
            $this->ReportModel->update_data($param, array("id" => $id));
        }
        $this->AdminOpenationModel->insertLog($login["uid"], 25, '删除举报内容：'.json_encode($idList));

        std_out(0, "");
    }

    /**
     * 处理举报内容
     */
    public function report_status(){
        $id = $this->input->get_post("id");
        $status = $this->input->get_post("status");
        $type = $this->input->get_post("type");
        $tag = $this->input->get_post("tag");
        $time = $this->input->get_post("time");
        if(empty($id)){
            std_out(-1, "id不能为空");
        }
        $login = getLoginUser();
        $param = array("status" => $status, "to_uid" => $login["uid"]);
        //查出此id里的所有的举报数据
        $data_list=$this->ReportModel->get_list_all($id);
        //取出当前消息属实的反馈 接收人
        if($status == 1){
            $this->be_report($id,$status,$login["uid"],$tag,$type,$time);
        }
        //循环处理
        $ids=array();
        $uid_arr=array();
        foreach ($data_list as $k=>$v){
            $this->handle_report($v,$status,$login["uid"],$tag,$type,$time,$uid_arr);
            $uid_arr[]=$v['uid'];
            $ids[]=$v['id'];
        }
        $this->AdminOpenationModel->insertLog($login["uid"], 25, '处理举报内容：'.json_encode(array("id" => $ids, "status" => $status)));
        std_out(0, "");
    }


    /**
     * 如果属实，为被举报人发消息
     * name: be_report
     * @param array $id
     * @param string $status
     * @param string $admin_uid
     * @param string $tag
     * @param string $type
     * @param string $time
     * @return bool
     * @throws Exception
     * @Author:taiyang
     */
    public function be_report($id,$status='',$admin_uid='',$tag='',$type='',$time=''){
        $reportTag = $this->ReportModel->tag;
        $punish = $this->ReportModel->punish;
        $punish_time = $this->ReportModel->punish_time;
        $data=$this->ReportModel->query_one(array('id'=>$id));
        if(empty($data)){ return false;}
        if($data['type'] == 1){//举报帖子
            if($status == 1){//10://被举报帖子属实—被举报人
                //取出帖子的uid
                $post_data=$this->PostModel->query_one(array('id'=>$data['be_id']));
                $this->NoticeModel->send_notice($post_data['uid'],$admin_uid,10,$data['type'],$data['be_id'],$reportTag[$tag]);
                //删除帖子
                $this->PostModel->del_post($data['be_id']);
                //task-event
                $this->PostModel->taskEventApi('be_report_post_true',array('uid'=>$post_data['uid'],'data'=>'be_report_post_true:'.$data['be_id']));

            }
        }
        if($data['type'] == 0){//举报用户
            if($status == 1){//11://被举报用户属实—被举报人
                $this->NoticeModel->send_notice($data['be_id'],$admin_uid,11,$data['type'],$data['be_id'],$reportTag[$tag],$punish[$type].$punish_time[$time]);
                // 处罚用户
                $this->UserModel->user_jin($type,$time,$data['be_id'],array('uid'=>$admin_uid));
                //task-event
                $this->PostModel->taskEventApi('be_report_user_true',array('uid'=>$data['be_id'],'data'=>'be_report_user_true:'.$data['be_id']));
            }

        }
        if($data['type'] == 2) {//举报评论
            if ($status == 1) {//12://被举报评论属实-被举报人
                $comment_data=$this->CommentModel->query_one(array('id'=>$data['be_id']));
                $this->NoticeModel->send_notice($comment_data['uid'], $admin_uid, 12, $data['type'], $data['be_id'], $reportTag[$tag]);
            // 删除评论
                $num='-1';
                $res=$this->CommentModel->update_data(array('status'=>$num),array('id'=>$data['be_id']));
                if($res !== false){
                    $this->CommentModel->update_count($data['be_id']);
                }
                //task-event
                $this->PostModel->taskEventApi('be_report_comm_true',array('uid'=>$comment_data['uid'],'data'=>'be_report_comm_true:'.$data['be_id']));
            }
        }


        return true;

    }

    /**
     * 举报用户，帖子，评论 审核后开始为举报人发送通知
     * name: handle_report
     * @param array $data
     * @param string $status
     * @param string $admin_uid
     * @param string $tag
     * @param string $type
     * @param string $time
     * @throws Exception
     * @Author:taiyang
     */
    public function handle_report($data=array(),$status='',$admin_uid='',$tag='',$type='',$time='',$uid_arr=array()){
        $reportTag = $this->ReportModel->tag;
        $punish = $this->ReportModel->punish;
        $punish_time = $this->ReportModel->punish_time;

        if(!in_array($data['uid'],$uid_arr)) {
            //举报帖子
            if ($data['type'] == 1) {
                if ($status == 1) {//4://举报用户帖子属—举报人
//                send_notice($uid='',$admin_uid='',$type=1,$related_type='',$related_id='',$reason='',$punish='')
                    $this->NoticeModel->send_notice($data['uid'], $admin_uid, 4, $data['type'], $data['be_id'], $reportTag[$tag], '');
                    //task-event
                    $this->PostModel->taskEventApi('report_post_true',array('uid'=>$data['uid'],'data'=>'report_post_true:'.$data['be_id']));

                }
                if ($status == -1) {//5://举报用户帖子不属—举报人
                    $this->NoticeModel->send_notice($data['uid'], $admin_uid, 5, $data['type'], $data['be_id']);
                    //task-event
                    $this->PostModel->taskEventApi('report_false',array('uid'=>$data['uid'],'data'=>'report_post_false:'.$data['be_id'],'report_type'=>'post'));
                }
            }

            //举报用户

            if ($data['type'] == 0) {
                if ($status == 1) {//6://举报用户属—举报人
                    $this->NoticeModel->send_notice($data['uid'], $admin_uid, 6, $data['type'], $data['be_id'], $reportTag[$tag], $punish[$type] . $punish_time[$time]);
                    //task-event
                    $this->PostModel->taskEventApi('report_user_true',array('uid'=>$data['uid'],'data'=>'report_user_true:'.$data['be_id']));
                }
                if ($status == -1) {//7://举报用户不属—举报人
                    $this->NoticeModel->send_notice($data['uid'], $admin_uid, 7, $data['type'], $data['be_id']);
                    //task-event
                    $this->PostModel->taskEventApi('report_false',array('uid'=>$data['uid'],'data'=>'report_user_false:'.$data['be_id'],'report_type'=>'user'));
                }
            }
            //举报评论

            if ($data['type'] == 2) {
                if ($status == 1) {//8://举报用户评论属—举报人
                    $this->NoticeModel->send_notice($data['uid'], $admin_uid, 8, $data['type'], $data['be_id'], $reportTag[$tag]);
                    //task-event
                    $this->PostModel->taskEventApi('report_comment_true',array('uid'=>$data['uid'],'data'=>'report_comment_true:'.$data['be_id']));

                }
                if ($status == -1) {//9://举报用户评论不属—举报人
                    $this->NoticeModel->send_notice($data['uid'], $admin_uid, 9, $data['type'], $data['be_id']);
                    //task-event
                    $this->PostModel->taskEventApi('report_false',array('uid'=>$data['uid'],'data'=>'report_comment_false:'.$data['be_id'],'report_type'=>'comment'));
                }
            }
        }
        //修改当前数据状态
        $param = array("status" => $status, "to_uid" => $admin_uid);
        $r=$this->ReportModel->update_data($param, array("id" => $data['id']));
        return true;


    }

}